package com.demo.jxls.util;

import com.demo.jxls.entity.Employee;
import lombok.SneakyThrows;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MainTest {

    @SneakyThrows
    public static void main(String[] args) {
        // 获取员工数据
        List<Employee> employees = getMockData();
        // 读取excel模板
        InputStream in = Files.newInputStream(new File("e:\\demo.xlsx").toPath());
        // 添加内容
        Context context = new Context();
        context.putVar("employees", employees);
        // 输出excel
        OutputStream out = Files.newOutputStream(new File("e:\\demo1.xlsx").toPath());
        // 调用模板方法
        JxlsHelper.getInstance().processTemplate(in, out, context);
    }

    public static List<Employee> getMockData() {
        List<Employee> employees = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        employees.add(new Employee("张三", "1990-10-11", new BigDecimal(1000), new BigDecimal(1500)));
        employees.add(new Employee("李四", "1991-10-11", new BigDecimal(1001), new BigDecimal(1501)));
        employees.add(new Employee("王五", "1992-10-11", new BigDecimal(1002), new BigDecimal(1502)));
        employees.add(new Employee("赵六", "1993-10-11", new BigDecimal(1003), new BigDecimal(1503)));
        return employees;
    }

}
